﻿using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;

namespace GMall.AdministrationService.Menus
{
    public class TreeMenu
    {
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public TreeMenuItemList Items { get; }
        public TreeMenu(string name, string displayName = null)
        {
            Name = name;
            DisplayName = displayName ?? Name;
            Items = new TreeMenuItemList();
        }
        public TreeMenu AddItem(TreeMenuItem menuItem)
        {
            Items.Add(menuItem);
            return this;
        }
    }

    public class TreeMenuItem
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public string Code { get; set; }
        public string Url { get; set; }
        public string Api { get; set; }
        public string Icon { get; set; }
        public int Order { get; set; }
        public virtual MenuType Type { get; set; }
        public bool IsActive { get; set; }
        public bool IsVisible { get; set; }
        public Guid? ParentId { get; set; }
        public string CustomData { get; set; }
        public DateTime CreationTime { get; set; }
        public bool IsLeaf => Items.IsNullOrEmpty();
        public TreeMenuItemList Items { get; }
        public TreeMenuItem(Guid id,string name, string displayName, string code, string url, string api, string icon, int order, MenuType type, bool isVisible, Guid? parentId, string customData)
        {
            Id = id;
            Name = name;
            DisplayName = displayName;
            Code = code;
            Url = url;
            Api = api;
            Icon = icon;
            Order = order;
            Type = type;
            IsVisible = isVisible;
            ParentId = parentId;
            CustomData = customData;
            CustomData = customData;
            Items = new TreeMenuItemList();
        }
        public TreeMenuItem AddItem([NotNull] TreeMenuItem menuItem)
        {
            Items.Add(menuItem);
            return this;
        }
    }

    public class TreeMenuItemList : List<TreeMenuItem>
    {
        public TreeMenuItemList()
        {
        }
        public TreeMenuItemList(int capacity)
            : base(capacity)
        {
        }
        public TreeMenuItemList(IEnumerable<TreeMenuItem> collection)
            : base(collection)
        {
        }
        public void Normalize()
        {
            RemoveEmptyItems();
            Order();
        }

        private void RemoveEmptyItems()
        {
            RemoveAll(item => item.IsLeaf && item.Url.IsNullOrEmpty());
        }

        private void Order()
        {
            //TODO: Is there any way that is more performant?
            var orderedItems = this.OrderBy(item => item.Order).ToArray();
            Clear();
            AddRange(orderedItems);
        }
    }
}
